package com.poi.demopoi.utils;

import com.poi.demopoi.exception.rtest.ResultStatus;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.poi.ss.formula.functions.T;

import java.io.Serializable;


/**
 * @Classname R
 * @Description 响应信息主体
 * @Author 李号东 lihaodongmail@163.com
 * @Date 2019-03-27 21:54
 * @Version 1.0
 */
@Setter
@Getter
@ToString
@Accessors(chain = true)
public class R2 implements Serializable {

    private static final long serialVersionUID = 1L;


    private int code = 200;
    private String msg;
    private Object data;

    private R2() {
    }

    private R2(ResultStatus resultStatus, Object data) {
        this.code = resultStatus.getCode();
        this.msg = resultStatus.getMessage();
        this.data = data;
    }

    //业务成功返回业务代码和描述信息
    public static R2 ok() {
        return new R2(ResultStatus.SUCCESS, null);
    }

    //业务成功返回业务代码,描述和返回的参数
    public static R2 ok(Object data) {
        return new R2(ResultStatus.SUCCESS, data);
    }

    //业务成功返回业务代码,描述和返回的参数
    public static R2 ok(ResultStatus resultStatus, T data) {
        if (resultStatus == null) {
            return ok(data);
        }
        return new R2(resultStatus, data);
    }

    //业务异常返回业务代码和描述信息
    public static R2 failure() {
        return new R2(ResultStatus.INTERNAL_SERVER_ERROR, null);
    }

    //业务异常返回业务代码,描述和返回的参数
    public static R2 failure(ResultStatus resultStatus) {
        return error(resultStatus, null);
    }

    //业务异常返回业务代码,描述和返回的参数
    public static R2 error(ResultStatus resultStatus, T data) {
        if (resultStatus == null) {
            return new R2(ResultStatus.INTERNAL_SERVER_ERROR, null);
        }
        return new R2(resultStatus, data);
    }


}
